//
//  TJTemplateMultipleDetailController.m
//  BaoDian
//
//  Created by mac on 2024/8/4.
//

#import "TJTemplateMultipleDetailController.h"
#import "SDCollectionTagsFlowLayout.h"
#import "TJTemplateTagCell.h"
#import "TJStyle1Button.h"
#import "TJTemplateMultipleDetailCell.h"
#import "SDCycleScrollView.h"
#import "TJTemplateDetailController.h"


@interface TJTemplateMultipleDetailController ()<UICollectionViewDataSource, UICollectionViewDelegate, SDCycleScrollViewDelegate>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSArray <TJTemplateTagModel *>*tags;
@property (nonatomic, weak) SDCycleScrollView *cycleScrollView;
@property (nonatomic, strong) NSMutableArray *datas;

@end

@implementation TJTemplateMultipleDetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.navigationBar.title = @"模版详情";

    UIView *headerView = [UIView addToView:self.view];
    headerView.backgroundColor = UIColorMake(0x1E1F23);
    
    SDCycleScrollView *cycleScrollView =  [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(16, KNavBarHight + 3, KScreenWidth - 32, 130) delegate:self placeholderImage:nil];
    cycleScrollView.addCornerRadius(12);
    cycleScrollView.backgroundColor = UIColor.tj_backgroundColor;
    cycleScrollView.bannerImageViewContentMode = UIViewContentModeScaleAspectFill;

    _cycleScrollView = cycleScrollView;
    [self.view addSubview:cycleScrollView];
 
    [self.tags enumerateObjectsUsingBlock:^(TJTemplateTagModel *  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        obj.width = [obj.text getStringSizeWithWidth:200 font:UIFontMake(12)].width + 16;
    }];
    
    [headerView addSubview:self.collectionView];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.inset(16);
        make.top.equalTo(cycleScrollView.mas_bottom).offset(16);
        make.height.offset(18);
    }];
    
    UILabel *textLabel = [UILabel lableWithSuperView:headerView font:UIFontSemiboldMake(16) color:UIColor.whiteColor title:self.model.videoName textAlignment:NSTextAlignmentLeft];
    textLabel.numberOfLines = 0;
    [textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.inset(16);
        make.top.equalTo(self.collectionView.mas_bottom).offset(10);
        make.bottom.inset(16);
    }];
    [headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.inset(16);
        make.top.offset(KNavBarHight + 3);
        make.bottom.equalTo(textLabel).offset(16);
    }];
    
    [self.tableView registerClass:TJTemplateMultipleDetailCell.class forCellReuseIdentifier:@"TJTemplateMultipleDetailCell"];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.rowHeight = 90;
    self.tableView.contentInset = UIEdgeInsetsMake(16, 0, 0, 0);
    self.tableView.backgroundColor = self.view.backgroundColor;
    [self.tableView setSectionIndexColor:UIColor.tj_9B9B9BColor];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(headerView.mas_bottom);
        make.left.right.bottom.offset(0);
    }];
    
    [self loadData];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.datas.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TJTemplateMultipleDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TJTemplateMultipleDetailCell" forIndexPath:indexPath];
    TJTemplateModel *model = self.datas[indexPath.row];
    cell.model = model;
    [cell setDidClickClockButtonBlock:^{
        TJTemplateDetailController *templateDetailController = [[TJTemplateDetailController alloc] init];
        templateDetailController.model = (TJTemplateModel *)model;
        [[TJTools currentNavController] pushViewController:templateDetailController];
    }];
    
 
    return cell;
}


- (void)didClickStartButton {
    
}

- (UICollectionView *)collectionView {
    if (!_collectionView) {
        
        SDCollectionTagsFlowLayout *flowLayout= [[SDCollectionTagsFlowLayout alloc] initWthType:TagsTypeWithLeft];
        flowLayout.minimumLineSpacing = 2;
        flowLayout.minimumInteritemSpacing = 8;
        flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.backgroundColor = UIColor.clearColor;
        [_collectionView registerClass:TJTemplateTagCell.class forCellWithReuseIdentifier:@"TJTemplateTagCell"];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
    }
    return _collectionView;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.tags.count;
}

- ( UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    TJTemplateTagCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TJTemplateTagCell" forIndexPath:indexPath];
    cell.model = self.tags[indexPath.row];
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    TJTemplateTagModel *model = self.tags[indexPath.row];
    return CGSizeMake(model.width, 18);
}

- (NSArray<TJTemplateTagModel *> *)tags {
    if (!_tags) {
        _tags = @[
            [TJTemplateTagModel modelWithBlock:^(TJTemplateTagModel * _Nonnull model) {
                model.text = self.model.dataDate;
            }],
            [TJTemplateTagModel modelWithBlock:^(TJTemplateTagModel * _Nonnull model) {
                model.text = [NSString stringWithFormat:@"%@人使用", self.model.usedTimes];
            }],
            [TJTemplateTagModel modelWithBlock:^(TJTemplateTagModel * _Nonnull model) {
                model.text = [NSString stringWithFormat:@"打卡%@天", self.model.clockTimes];
            }],
        ];
    }
    return _tags;
}
 
- (NSMutableArray *)datas {
    if (!_datas) {
        _datas = [NSMutableArray array];
    }
    return _datas;
}

- (void)loadData {
    [TJTemplatePresent loadCarouselDataComplete:^(NSArray * _Nonnull datas, NSError * _Nullable error) {
        NSMutableArray *arr = [NSMutableArray array];
        for (NSDictionary *dict in datas) {
            [arr addObject:dict[@"imgUrl"]];
        }
        self.cycleScrollView.imageURLStringsGroup = arr;
    }];
    
    [TJTemplatePresent loadTempleteGatherDataWithId:self.model.Id pageIndex:1 complete:^(NSArray * _Nonnull datas, NSError * _Nullable error) {
       
        [self.datas removeAllObjects];
        if (datas) {
            [self.datas addObjectsFromArray:datas];
        }
        [self.tableView reloadData];
    }];
}

@end
